package com.canbot.u05.view;


import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.canbot.u05.R;

import java.lang.reflect.Field;
import java.util.Calendar;

public class TimePickerDialog extends Dialog {

        private Context mContext;

        private Button positiveButton, negativeButton;

        private TextView title;

        private Calendar calendar;

        private int mHour;

        private int mMinute;

        private StringBuilder stringBuilder;

        private TimePicker timePicker;

        public TimePickerDialog(Context context) {
                super(context, R.style.MyDialogStyle);
                this.mContext = context;
                setCustomDialog();

        }

        private void setCustomDialog() {
                View mView = LayoutInflater.from(getContext()).inflate(R.layout.layout_time_picker, null);
                title = (TextView) mView.findViewById(R.id.title);
                timePicker = (TimePicker) mView.findViewById(R.id.time_picker);
                //初始化时间
                positiveButton = (Button) mView.findViewById(R.id.sure);
                negativeButton = (Button) mView.findViewById(R.id.cancle);

                super.setContentView(mView);


        }

        private void set_timepicker_text_colour(TimePicker time_picker){
                Resources system = Resources.getSystem();
                int hour_numberpicker_id = system.getIdentifier("hour", "id", "android");
                int minute_numberpicker_id = system.getIdentifier("minute", "id", "android");
                int ampm_numberpicker_id = system.getIdentifier("amPm", "id", "android");

                NumberPicker hour_numberpicker = (NumberPicker) time_picker.findViewById(hour_numberpicker_id);
                NumberPicker minute_numberpicker = (NumberPicker) time_picker.findViewById(minute_numberpicker_id);
                NumberPicker ampm_numberpicker = (NumberPicker) time_picker.findViewById(ampm_numberpicker_id);

                set_numberpicker_text_colour(hour_numberpicker);
                set_numberpicker_text_colour(minute_numberpicker);
                set_numberpicker_text_colour(ampm_numberpicker);
        }
        private void set_numberpicker_text_colour(NumberPicker number_picker){
                final int count = number_picker.getChildCount();
                //这里就是要设置的颜色，修改一下作为参数传入会更好
                final int color = mContext.getResources().getColor(android.R.color.black);

                for(int i = 0; i < count; i++){
                        View child = number_picker.getChildAt(i);

                        try{
                                Field wheelpaint_field = number_picker.getClass().getDeclaredField("mSelectorWheelPaint");
                                wheelpaint_field.setAccessible(true);

                                ((Paint)wheelpaint_field.get(number_picker)).setColor(color);
                                ((EditText)child).setTextColor(color);
                                number_picker.invalidate();
                        }
                        catch(NoSuchFieldException e){

                        }
                        catch(IllegalAccessException e){

                        }
                        catch(IllegalArgumentException e){

                        }
                }
        }


        public void setTitle(String text) {
                title.setText(text);
        }

        public void setTime(int hour,int minute){
                set_timepicker_text_colour(timePicker);
                timePicker.setIs24HourView(true);
                timePicker.setHour(hour);
                timePicker.setMinute(minute);
        }


        @Override
        public void setContentView(int layoutResID) {
        }

        @Override
        public void setContentView(View view, LayoutParams params) {
        }

        @Override
        public void setContentView(View view) {
        }


        /**
         * 确定键监听器
         *
         * @param listener
         */
        public void setOnPositiveListener(View.OnClickListener listener) {
                positiveButton.setOnClickListener(listener);

        }

        /**
         * 取消键监听器
         *
         * @param listener
         */
        public void setOnNegativeListener(View.OnClickListener listener) {
                negativeButton.setOnClickListener(listener);
        }

        public String getTime(){
                mHour = timePicker.getHour();
                mMinute = timePicker.getMinute();
                //时间小于10的数字 前面补0 如01:12:00
                return new StringBuilder().append(mHour < 10 ? "0" + mHour : mHour).append(":")
                        .append(mMinute < 10 ? "0" + mMinute : mMinute).toString();
        }

}
